package com.fary.context.support;

import com.fary.beans.factory.BeanNameAware;
import com.fary.beans.factory.InitializingBean;
import com.fary.context.ApplicationContext;
import com.fary.util.Assert;

public abstract class AbstractRefreshableConfigApplicationContext extends AbstractRefreshableApplicationContext
		implements BeanNameAware, InitializingBean {

	private String[] configLocations;

	public AbstractRefreshableConfigApplicationContext() {
	}

	public AbstractRefreshableConfigApplicationContext(ApplicationContext parent) {
		super(parent);
	}

	public void setConfigLocations(String... locations) {
		if (locations != null) {
			Assert.noNullElements(locations, "Config locations must not be null");
			this.configLocations = new String[locations.length];
			for (int i = 0; i < locations.length; i++) {
				this.configLocations[i] = resolvePath(locations[i]).trim();
			}
		} else {
			this.configLocations = null;
		}
	}

	protected String resolvePath(String path) {
		return getEnvironment().resolveRequiredPlaceholders(path);
	}

	protected String[] getConfigLocations() {
		return (this.configLocations != null ? this.configLocations : getDefaultConfigLocations());
	}

	protected String[] getDefaultConfigLocations() {
		return null;
	}


}